import React, { Component } from "react";
import { changeAge, changeUser } from '../store/actionCreate';//引入行为函数
import { reducer } from '../store/reducer'; //引入reducer
import { createStore } from '../store/action'; //引入action

const store = createStore(reducer);
class Home extends React.Component{
    componentWillMount(){
        store.subscribe(this.update);
        this.update();
        // this.props.saveImg("asdasdasd");
    }
    update = () => {
        console.log(this)
        this.setState(store.getState());
    }
    onChange = (e) => {
        store.dispatch(changeUser(e.target.value));
    }
    onClick = () => {
        store.dispatch(changeAge());
    }
    render(){
        return (
            <div>
                <p>user: {this.state.user}, age: {this.state.age}</p>
                user:
                <input type="text" className="input" onChange={this.onChange}/>
                &nbsp;
                <button className="btn" onClick={this.onClick}>年龄增长</button>
            </div>
        );
    }
}

export default Home;


